public class SingleLinkedList {
    public static void main(String[] args) {
        SingleLinkedListUse list = new SingleLinkedListUse();
        list.SLTPushFront(1);
        list.SLTPushFront(2);
        list.SLTPushFront(3);
        list.SLTPushFront(4);
        list.SLTPushBack(5);
        list.SLTPushBack(6);
        //list.SLTPrint();
        //System.out.println(list.SLTLength());
        //list.deletePosNode(100);
        list.SLTPopBack();
        list.SLTPopFront();
        list.SLTPopFront();
        list.SLTPopFront();
        list.SLTPrint();
        System.out.println(list.SLTLength());
        list = null;
    }
}

class SingleLinkedListNode {
    int data;
    SingleLinkedListNode next;
    public SingleLinkedListNode(int val) {
        this.data = val;
    }
}

class SingleLinkedListUse {
    public SingleLinkedListNode head;

    public void SLTPushFront(int val) {
        SingleLinkedListNode newNode = new SingleLinkedListNode(val);
        if ( this.head == null) {
            this.head = newNode;
            return;
        }
        newNode.next = head;
        head = newNode;
    }

    public void SLTPushBack(int val) {
        SingleLinkedListNode newNode = new SingleLinkedListNode(val);
        if (this.head == null) {
            this.head = newNode;
            return;
        }
        SingleLinkedListNode cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
    }

    public int SLTLength() {
        SingleLinkedListNode cur = head;
        int length = 0;
        while (cur != null) {
            length++;
            cur = cur.next;
        }
        return length;
    }

    public void deletePosNode(int index) {
        if (head == null) {
            System.out.println("Wrong Operation!!!");
            return;
        }
        --index;
        SingleLinkedListNode cur = this.head;
        SingleLinkedListNode prev = cur;
        while ((index--) != 0) {
            if (cur == null) {
                System.out.println("Out of Index!");
                return;
            }
            prev = cur;
            cur = cur.next;
        }
        prev.next = cur.next;
        cur.next = null;
    }

    public void SLTPopBack(){
        if (this.head == null) {
            System.out.println("List is already cleared!");
            return;
        }
        SingleLinkedListNode cur = this.head;
        int cnt = 1;
        while (cur.next != null) {
            cur = cur.next;
            cnt++;
        }
        deletePosNode(cnt);
    }

    public void SLTPopFront() {
        if (this.head == null) {
            System.out.println("List is already cleared!");
            return;
        }
        SingleLinkedListNode cur = this.head;
        this.head = this.head.next;
        cur.next = null;
    }

    public void SLTPrint() {
        SingleLinkedListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
    }
}